package zabbix

import (
	"encoding/json"
	"errors"
)

func CorrelationCreateData() (string,map[string]interface{}) {
	method := "Correlation.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "new event tag correlation",
        "filter": {
            "evaltype": 0,
            "conditions": [
                {
                    "type": 1,
                    "tag": "ok"
                }
            ]
        },
        "operations": [
            {
                "type": 0
            }
        ]	
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func CorrelationDeleteData() (string,map[string]interface{}) {
	method := "Correlation.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "1",
        "2"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func CorrelationUpdateData() (string,map[string]interface{}) {
	method := "Correlation.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "correlationid": "1",
        "status": "1"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func CorrelationGetData() (string,map[string]interface{}) {
	method := "Correlation.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectOperations": "extend",
        "selectFilter": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func CorrelationDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := CorrelationDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func CorrelationUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := CorrelationUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func CorrelationGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := CorrelationGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func CorrelationCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := CorrelationCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


